package avicit.bdp.dgs.metadata.service;

import avicit.bdp.common.base.BaseService;
import avicit.bdp.dgs.metadata.dao.MetadataBaseVersionDao;
import avicit.bdp.dgs.metadata.dto.MetadataBaseVersionDTO;
import avicit.platform6.commons.utils.ComUtil;
import avicit.platform6.core.exception.DaoException;
import avicit.platform6.modules.system.syslog.service.SysLogUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class MetadataBaseVersionService
    extends BaseService<MetadataBaseVersionDao, MetadataBaseVersionDTO> {

  /**
   * 元数据第一次采集，自动为该数据源创建一个基线版本
   *
   * @param dto 保存对象
   * @return String
   */
  @Transactional
  public String save(MetadataBaseVersionDTO dto) {
    try {
      dto.setId(ComUtil.getId());
      this.insertWithoutVersion(dto);
      // 记录日志
      SysLogUtil.log4Insert(dto);
      return dto.getId();
    } catch (Exception e) {
      throw new DaoException(e.getMessage(), e);
    }
  }

  public MetadataBaseVersionDTO getOneByDatasourceId(String datasourceId) {
    MetadataBaseVersionDTO query = new MetadataBaseVersionDTO();
    query.setDatasourceId(datasourceId);
    return this.selectOne(query);
  }

}
